package timetracker.client.forms;

import java.util.List;

import timetracker.client.ContragentService;
import timetracker.client.ContragentServiceAsync;
import timetracker.client.database.Contragent;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class ContragentList extends DialogBox {

	private static ContragentListUiBinder uiBinder = GWT
			.create(ContragentListUiBinder.class);
	@UiField(provided=true) CellTable<Contragent> contragentTable = new CellTable<Contragent>();
	@UiField Button btnSelect;
	@UiField Button btnAdd;
	@UiField Button btnEdit;
	@UiField Button btnDelete;
	@UiField Button btnUpdate;
	@UiField Button btnClose;
	
	private Contragent selectedValue;
	private Boolean valueIsChosen = false;
	
	final SingleSelectionModel<Contragent> selectionModel = new SingleSelectionModel<Contragent>();
	
	public Contragent getChosenValue(){
		return selectedValue;
	}
	
	public void setChosenValue(Contragent value){
		selectedValue = value;
	}
	
	public Boolean valueIsChosen(){
		return valueIsChosen;
	}
	
	interface ContragentListUiBinder extends UiBinder<Widget, ContragentList> {
	}

	private final ContragentServiceAsync contragentService = GWT.create(ContragentService.class);
	AsyncCallback<List<Contragent>> callback = new AsyncCallback<List<Contragent>>() {

		@Override
		public void onFailure(Throwable caught) {
			String details = caught.getMessage();
			Window.alert(details);
		}

		@Override
		public void onSuccess(List<Contragent> contragents) {
			updateListData(contragents);
		}
	};
	public void updateList(){
		contragentService.getAllContragents(callback);
	}
	
	public void updateListData(List<Contragent> contragents){
		contragentTable.setRowData(0, contragents);
		if (selectedValue != null){
			selectionModel.setSelected(selectedValue, true);
		}
	}
	
	public ContragentList() {
		TextColumn<Contragent> idColumn = new TextColumn<Contragent>() {
		      @Override
		      public String getValue(Contragent contragent) {
		        return contragent.getKey().toString();
		      }
		    };
		TextColumn<Contragent> nameColumn = new TextColumn<Contragent>() {
		      @Override
		      public String getValue(Contragent contragent) {
		        return contragent.getName();
		      }
		    };
		
		contragentTable.addColumn(idColumn, "ID");
		contragentTable.addColumn(nameColumn, "Name");
		
		contragentTable.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
		contragentTable.setSelectionModel(selectionModel);
		selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
		      public void onSelectionChange(SelectionChangeEvent event) {
		        selectedValue = selectionModel.getSelectedObject();
		      }
		    });
		
		setWidget(uiBinder.createAndBindUi(this));
		setText("Contragents");
		updateList();
	}
	
	@UiHandler("btnSelect")
	void onBtnSelectClick(ClickEvent event) {
		if (selectedValue != null) {
			valueIsChosen = true;
			hide();
	    } 
		
	}
	
	@UiHandler("btnAdd")
	void onBtnAddClick(ClickEvent event) {
		final ContragentForm dialog = new ContragentForm();
		dialog.center();
		updateList();
	}
	
	@UiHandler("btnEdit")
	void onBtnEditClick(ClickEvent event) {
		if (selectedValue == null) {
			return;
	    }
		final ContragentForm dialog = new ContragentForm();
		dialog.setContragent(selectedValue);
		dialog.center();
		updateList();
	}
	
	@UiHandler("btnDelete")
	void onBtnDeleteClick(ClickEvent event) {
		if (selectedValue == null) {
			return;
	    }
		/*final ContragentForm dialog = new ContragentForm();
		dialog.setContragent(selectedRow);
		dialog.center();
		updateList();*/
	}
	
	@UiHandler("btnUpdate")
	void onBtnUpdateClick(ClickEvent event) {
		updateList();
	}

	@UiHandler("btnClose")
	void onBtnCloseClick(ClickEvent event) {
		hide();
	}
}
